﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class Contacts : System.Web.UI.Page
    {
        private static ContactBO contactbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((Session["LOGIN"] == null) || (int)Session["ROLES"] != 1)
            //{
            //    Response.Redirect("Login.aspx");
            //}

            string mode = Request.QueryString["Mode"];
            //Mode Add
            if (mode == "1")
            {
                btnUpdate.Visible = false;
                btnDelete.Visible = false;
                txtcontactid.Visible = false;
                lblcontactID.Visible = false;
                PanelActions.Visible = true;
                lbHeaderActions.Text = "Thêm danh sách";
                GridView1.Columns[0].Visible = false;

            }
            else //Mode Update
            {
                btnAdd.Visible = false;
                lbHeaderActions.Text = "Sửa danh sách";
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {



            //CheckBox checkbox = new CheckBox();
            int contactid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
            contactbo = ContactBO.GetContactBO(contactid);
            txtcontactid.Text = contactbo.ContactID.ToString();
            txtname.Text = contactbo.Name.ToString();
            txtisyahoo.Text = contactbo.IsYahoo;
            txtisskyer.Text = contactbo.IsSkyper;
            txtemail.Text = contactbo.Email;
            txttitle.Text = contactbo.Title;
            txtcontactcontent.Text = contactbo.ContactContent;
            txtcreateddate.Text = contactbo.CreadedDate.Value.ToString("dd/MM/yyyy");
            txtupdatedate.Text = contactbo.UpdatedDate.Value.ToString("dd/MM/yyyy");
            ischeck.Checked = contactbo.IsCheck;
            isactive.Checked = contactbo.IsActive;
            message.Text = "";
            if (PanelActions.Visible == false)
                PanelActions.Visible = true;
            messagedelete.Text = "";
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            int contactid = Convert.ToInt32(txtcontactid.Text);
            contactbo.Name = txtname.Text;
            contactbo.IsYahoo = txtisyahoo.Text;
            contactbo.IsSkyper = txtisskyer.Text;
            contactbo.Email = txtemail.Text;
            contactbo.Title = txttitle.Text;
            contactbo.ContactContent = txtcontactcontent.Text;
            //contactbo.CreadedDate = txtcreateddate.Text;
            //contactbo.UpdatedDate = txtupdatedate.Text;
            //txtcreateddate.Text = contactbo.CreadedDate.Value.ToString("dd/MM/yyyy");
            //txtupdatedate.Text=contactbo.UpdatedDate.Value.ToString("dd/MM/yyyy");
            contactbo.IsCheck = ischeck.Checked;
            contactbo.IsActive = isactive.Checked;

            try
            {
                contactbo.Save();
                GridView1.DataBind();

                message.Text = "Cập nhật thành công !";
               // GridView1_SelectedIndexChanged(null, null);
            }
            catch
            {
                message.Text = "Cập nhật thất bại !";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int contactid = Convert.ToInt32(txtcontactid.Text);
            ContactBO.DeleteContactBO(contactid);
            GridView1.DataBind();
            messagedelete.Text = "Xóa thành công !";
            PanelActions.Visible = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
            ContactBO contactbo = ContactBO.NewContactBO();
            contactbo.Name = txtname.Text;
            contactbo.IsYahoo = txtisyahoo.Text;
            contactbo.IsSkyper = txtisskyer.Text;
            contactbo.Email = txtemail.Text;
            contactbo.Title = txttitle.Text;
            contactbo.ContactContent = txtcontactcontent.Text;
            //DateTime createddate = Convert.ToDateTime(txtcreateddate.Text);
            contactbo.CreadedDate = DateTime.Now;
            contactbo.UpdatedDate= DateTime.Now;
            //txtcreateddate.Text = contactbo.CreadedDate.Value.ToString("dd/MM/yyyy");
           // txtupdatedate.Text = contactbo.UpdatedDate.Value.ToString("dd/MM/yyyy");
            contactbo.IsCheck = ischeck.Checked;
            contactbo.IsActive = isactive.Checked;
            contactbo.Save();
            message.Text = "Thêm thành công !";
            GridView1.DataBind();
        }


        protected void txtcontactcontent_TextChanged(object sender, EventArgs e)
        {


        }

       
    }
}
